/// Ajax Busy
(function ($, undefined) {

    // define the name of the UI component
    var ui_name = "ajaxBusy";

    // initialize object
    var ab = $.ajaxBusy = {

        // default values
        defaults: {
            'timeOut': 20000, // 超时时间 默认20秒
            'loadingDelay': 200, // loading时间超过200ms才显示loading
            'hidingDelay' :300,
            'message': '载入中...'
        },

        setDefaults: function (defaultOptions) {
            this.defaults = $.extend({}, this.defaults, defaultOptions);
        }
    };

    $.fn.ajaxBusy = function (options) {
        var ajaxBusyCount = 0;
        var panelStr = '<div id="ajax-busy-cover" class="ajax-busy"><div class="icon-loading"></div><span/></div>';
        var backgroundStr = '<div id="overlay"/>';
        var defaultBusyString = '载入中...';
        var defaultTimeoutString = '虽然已经过去了很长时间，但是页面还没有加载完呢！请检查检查网络状况，或者试着重新打开这个页面吧。';

        // method calling
        if (typeof options == 'string') {
            var args = Array.prototype.slice.call(arguments, 1),
			res;
            this.each(function () {
                var data = $.data(this, ui_name);
                if (data) {
                    var meth = data[options];
                    if (meth) {
                        var r = meth.apply(this, args);
                        if (res === undefined) {
                            res = r;
                        }
                    }
                }
            });
            if (res !== undefined) {
                return res;
            }
            return this;
        }

        // initialize options
        options = $.extend({}, ab.defaults, options);

        this.each(function () {
            var _self = this;

            // initialize callback
            for (var key in options) {
                var value = options[key];
                if (typeof value == 'function') {
                    // bind callbacks
                    $(_self).bind(key, value);
                }
            }


            function getAjaxCover() {
                var divAjaxCover = $('#ajax-busy-cover,#overlay');

                if (divAjaxCover.length == 0) {
                    var divAjaxPanel = $(panelStr);
                    var divAjaxBackground = $(backgroundStr);

                    divAjaxCover = divAjaxCover
                                .add(divAjaxPanel)
                                .add(divAjaxBackground)
                                .hide()
                                .appendTo('body')
                                .fadeIn();
                    // 居中显示坐标

                    setInterval(function () {
                        divAjaxPanel.css({ left: ($(window).width() - divAjaxPanel.outerWidth()) / 2 });
                    }, 50);
                }
                return divAjaxCover;
            };
            /* Public Methods
            -----------------------------------------------------------------------------*/

            function showCover(message) {
                var divAjaxCover = getAjaxCover();
                if (message)
                    $('span', divAjaxCover).html(message);
                else if (ajaxBusyCount == 0)
                    $('span', divAjaxCover).html(options['message']);

                ajaxBusyCount++;
                setTimeout(function () {
                    if (ajaxBusyCount > 0) {
                        divAjaxCover.fadeIn();
                    }
                }, options['loadingDelay']);
            }
            function hideCover() {
                ajaxBusyCount--;
                setTimeout(function () {
                    if (ajaxBusyCount == 0) {
                        getAjaxCover().fadeOut();
                    }
                }, options['hidingDelay']);
            }

            function timeout() {
                if (ajaxBusyCount > 0) {
                    var divAjaxCover = getAjaxCover();
                    divAjaxCover.hide();
                    $('span', divAjaxCover).html(defaultTimeoutString);
                    divAjaxCover.fadeIn();
                }
            };



            /* Public Methods
            -----------------------------------------------------------------------------*/

            var publicMethods = {
                option: function (name, value) {
                    if (value === undefined) {
                        return options[name];
                    }
                    options[name] = value;
                },

                begin: showCover,

                timeout: timeout,

                end: hideCover

                // @other public methods
            };
            $.data(this, ui_name, publicMethods);
        });

        return this;
    };

    // @static methods

})(jQuery);

$.AjaxBusy = {};
$.AjaxBusy.run = function (method, message) {
    $.AjaxBusy.show(message);
    method();
    $.AjaxBusy.hide();
}
$.AjaxBusy.show = function (message) {
    $(document).ajaxBusy('begin', message);

}
$.AjaxBusy.hide = function () {
    $(document).ajaxBusy('end');
}

$(function () {
    $(document).ajaxBusy();
});
